<?php

namespace App\Http\Controllers\Admin\Articles;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Admin\Articles\Api\ArticlesApi;
use App\Http\Controllers\Admin\Categories\Api\CategoriesApi;
use App\Http\Controllers\Admin\Tags\Api\TagsApi;
use App\Http\Controllers\Common\CommonApi;
use App\Model\Articles;

class ArticlesController extends Controller
{   
    /**
     * 加载博客列表页面
     * @return 博客列表页面
     */
    public function articleList(Request $request)
    {   
        $articlesList = ArticlesApi::getArticlesList();

        //分页,8个一页
        $articlesList = CommonApi::CustomPagination($request, $articlesList, 8);
        // dd($articlesList);
        return view('Admin/Index/index', [
            'articlesList' => $articlesList,
            ]);
    }


    /**
     * 加载写博客页面
     * @return 写博客页面
     */
    public function articleAdd()
    {   
        //获取分类列表
        $categoriesList = CategoriesApi::getCategoriesList('active');
        //获取标签列表
        $tagsList = TagsApi::getTagsList();

        return view('Admin/Articles/add', [

                'categoriesList' => $categoriesList,
                'tagsList' => $tagsList,
            ]);
    }

    /**
     * 加载编辑博客页面
     * @return 编辑博客页面
     */
    public function articleEdit($id)
    {   
        //获取博客信息
        $article = Articles::find($id)->toArray();
        //获取分类列表
        $categoriesList = CategoriesApi::getCategoriesList('active');
        //获取标签列表
        $tagsList = TagsApi::getTagsList();
        //获取该博客所拥有的标签(id)
        $ownTagsID = \DB::table('articles_tags')->where('article_id', $id)->pluck('tag_id')->toArray();
        // dd(html_entity_decode($article["html"]));
        return view('Admin/Articles/edit', [

                'article' => $article,
                'categoriesList' => $categoriesList,
                'tagsList' => $tagsList,
                'ownTagsID' => $ownTagsID,
            ]);
    }

    /**
     * 执行编辑博客
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function doArticleUpdate(Request $request)
    {
        //输入的数据验证   
        $this->validate($request, [

            'category' => 'bail|required',
            'tags' => 'bail|required',
            'title' => 'required|max:100',
            'author' => 'required|max:15',

            ], [

                'category.required' => '请选择分类',
                'tags.required' => '请选择标签',
                'title.required' => '请填写标题',
                'title.max' => '标题不能超过100个字符',
                'author.max' => '作者名称不能超过15个字符',

            ]);

        //执行博客修改
        $result = ArticlesApi::doArticleUpdate($request);

        if ($result['code'] == 400) {
            
            return back()->with([

                'tipscode' => $result['code'],
                'tips' => $result['msg'],
                ]);

        } else if ($result['code'] == 200) {

            return Redirect('admin/article-list')->with([

                'tipscode' => $result['code'],
                'tips' => $result['msg'],
                ]);
        }
    }


    /**
     * 执行博客写入数据库
     * @return [type] [description]
     */
    public function doArticleAdd(Request $request)
    {
        
        // dd($request->input('html'), $request->input('content'));
        // dd(\Session::getId());
        // dd($request->file('cover'));
        
        //输入的数据验证   
        $this->validate($request, [

            'category' => 'bail|required',
            'tags' => 'bail|required',
            'title' => 'required|max:100',
            'author' => 'required|max:15',

            ], [

                'category.required' => '请选择分类',
                'tags.required' => '请选择标签',
                'title.required' => '请填写标题',
                'title.max' => '标题不能超过100个字符',
                'author.max' => '作者名称不能超过15个字符',

            ]);

        //执行录入数据库
        $result = ArticlesApi::doArticleAdd($request);

        if ($result['code'] == 400) {
            
            return back()->with([

                'tipscode' => $result['code'],
                'tips' => $result['msg'],
                ]);

        } else if ($result['code'] == 200) {

            return Redirect('admin/article-list')->with([

                'tipscode' => $result['code'],
                'tips' => $result['msg'],
                ]);
        }


    }


    
}
